package com.wan.word;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;

import com.umeng.analytics.MobclickAgent;
import com.wan.tool.InAndOut;

import java.util.ArrayList;
import java.util.List;

public class DialogActivity extends Activity {

    public int taskId = 0;
    public int start = 0;
    public int dayCount = 60;
    public int seekProgress = 0;
    public InAndOut.TaskInfo taskInfo = null;
    public List<View> taskButtonList = new ArrayList<View>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.taskdialog);
        findViewById(R.id.task_subimit).setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                openShowWord();
            }
        });
        findViewById(R.id.task_subimit_useold).setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                goOnOldTask();
            }
        });
        InAndOut.init(getSharedPreferences("word07", MODE_PRIVATE));
        taskButtonInit();
        dayButtonInit();
        seekBarInit();
        flushText();

    }

    private void seekBarInit() {
        SeekBar sbar = (SeekBar) findViewById(R.id.seekBar1);
        sbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                seekProgress = progress;
                flushText();
            }
        });
    }

    private void taskButtonInit() {
        View cet4 = findViewById(R.id.word_cet4);
        cet4.setTag(InAndOut.TASK_CET4);
        taskButtonList.add(cet4);
        View cet6 = findViewById(R.id.word_cet6);
        cet6.setTag(InAndOut.TASK_CET6);
        taskButtonList.add(cet6);
        View xgn1 = findViewById(R.id.word_xgn1);
        xgn1.setTag(InAndOut.TASK_xgn1);
        taskButtonList.add(xgn1);
        View xgn2 = findViewById(R.id.word_xgn2);
        xgn2.setTag(InAndOut.TASK_xgn2);
        taskButtonList.add(xgn2);
        View xgn3 = findViewById(R.id.word_xgn3);
        xgn3.setTag(InAndOut.TASK_xgn3);
        taskButtonList.add(xgn3);
        View xgn4 = findViewById(R.id.word_xgn4);
        xgn4.setTag(InAndOut.TASK_xgn4);
        taskButtonList.add(xgn4);
        View gre = findViewById(R.id.word_gre);
        gre.setTag(InAndOut.TASK_GRE);
        taskButtonList.add(gre);
        View tuofu = findViewById(R.id.word_tuofu);
        tuofu.setTag(InAndOut.TASK_TUOFU);
        taskButtonList.add(tuofu);
        View jisuanji = findViewById(R.id.word_jisuanji);
        jisuanji.setTag(InAndOut.TASK_JISUANJI);
        taskButtonList.add(jisuanji);
        View yasi = findViewById(R.id.word_yasi);
        yasi.setTag(InAndOut.TASK_YASI);
        taskButtonList.add(yasi);
        View yanjiusheng = findViewById(R.id.word_yanjiusheng);
        yanjiusheng.setTag(InAndOut.TASK_JISUANJI);
        taskButtonList.add(yanjiusheng);
        View yanjiushenghexin = findViewById(R.id.word_yanjiushenghexin);
        yanjiushenghexin.setTag(InAndOut.TASK_YANJIUSHENGHEXING);

        taskButtonList.add(yanjiushenghexin);
        for (View oneView : taskButtonList) {
            oneView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    for (View one : taskButtonList) {
                        one.setEnabled(true);
                    }
                    view.setEnabled(false);
                    taskId = (Integer) view.getTag();
                    taskInfo = InAndOut.TaskInfo.getTaskInfo(taskId);
                    flushText();
                }
            });
        }
        //默认cet4
        cet4.setEnabled(false);
        taskId = (Integer) cet4.getTag();
        taskInfo = InAndOut.TaskInfo.getTaskInfo(taskId);

    }

    private void dayButtonInit() {
        final Button dayWordCount1 = (Button) findViewById(R.id.daywordcout1);
        final Button dayWordCount2 = (Button) findViewById(R.id.daywordcout2);
        final Button dayWordCount3 = (Button) findViewById(R.id.daywordcout3);
        final Button dayWordCount4 = (Button) findViewById(R.id.daywordcout4);
        //默认第二个选中
        dayWordCount2.setEnabled(false);
        dayCount = 80;
        dayWordCount1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                dayCount = 60;
                dayWordCount1.setEnabled(false);
                dayWordCount2.setEnabled(true);
                dayWordCount3.setEnabled(true);
                dayWordCount4.setEnabled(true);
                flushText();
            }
        });
        dayWordCount2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                dayCount = 80;
                dayWordCount1.setEnabled(true);
                dayWordCount2.setEnabled(false);
                dayWordCount3.setEnabled(true);
                dayWordCount4.setEnabled(true);
                flushText();
            }
        });
        dayWordCount3.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                dayCount = 100;
                dayWordCount1.setEnabled(true);
                dayWordCount2.setEnabled(true);
                dayWordCount3.setEnabled(false);
                dayWordCount4.setEnabled(true);
                flushText();
            }
        });
        dayWordCount4.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                dayCount = 120;
                dayWordCount1.setEnabled(true);
                dayWordCount2.setEnabled(true);
                dayWordCount3.setEnabled(true);
                dayWordCount4.setEnabled(false);
                flushText();
            }
        });
    }

    private void openShowWord() {
//        //for test
//        start = taskInfo.wordCount - 10;
//        dayCount = 3;
//        //end for test
        InAndOut.TaskInfo.createTaskInfo(taskId, start, dayCount);
        Intent myIntent = new Intent();
        setResult(RESULT_OK, myIntent);
        finish();
    }

    private void goOnOldTask() {
        InAndOut.TaskInfo.goOnTaskInfo(taskId);
        Intent myIntent = new Intent();
        setResult(RESULT_OK, myIntent);
        finish();
    }

    private void flushText() {
        if (taskInfo == null) {
            return;
        }
        //起点进行限制，里面所有的值都从0开始。
        start = (int) (taskInfo.wordCount * (seekProgress / 100.0));
        if (start >= taskInfo.wordCount) {
            start = taskInfo.wordCount - 1;
        }
        if (start < 0) {
            start = 0;
        }

        TextView wordStartText = (TextView) findViewById(R.id.wordstarttext);
        wordStartText.setText("单词起点：" + (start + 1));

        String text = "单词" + (taskInfo.wordCount - start) + "个,要记忆" + ((taskInfo.wordCount - start + dayCount - 1) / dayCount) + "天.\n";
        if (taskInfo.nowIndex != 0 && !taskInfo.isAllFinish()) {
            text += "旧记录：\n起点：" + (taskInfo.start + 1) + ",已经记忆" + (taskInfo.nowIndex + taskInfo.nowDay * taskInfo.dayCount) + "个单词。";
        }
        TextView tasktest = (TextView) findViewById(R.id.dialog_flushText);
        tasktest.setText(text);

        if ((taskInfo.nowIndex != 0 || taskInfo.nowDay != 0) && !taskInfo.isAllFinish()) {
            findViewById(R.id.task_subimit_useold).setVisibility(View.VISIBLE);
        } else {
            findViewById(R.id.task_subimit_useold).setVisibility(View.GONE);
        }

    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.activity_left_in, R.anim.activity_right_out);
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }
}
